home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / Developer University / DUProjects / TalkerLib C++ Source / TTalker.cp < prev    next >
Encoding:
Text File  |  1995-03-10  |  4.6 KB  |  189 lines  |  [TEXT/CWIE]

  1. // TTalker.cp
  2. // Copyright © 1994 Apple Computer Inc. All rights reserved.
  3.  
  4. //=======================================================================
  5. #ifndef _TTALKER_
  6.     #include "TTalker.h"
  7. #endif
  8.  
  9. #ifndef _TCOMPUTER_
  10.     #include "TComputer.h"
  11. #endif
  12.     
  13. #ifndef __STRINGS__
  14.     #include "Strings.h"    // c2pstr, p2cstr
  15. #endif
  16.  
  17. #ifndef _STRING
  18.     #include "String.h"        // strcpy
  19. #endif
  20.  
  21. #ifndef __FP__
  22.     #include <FP.h>            // num2dec
  23. #endif
  24.  
  25. #ifndef NIL
  26.     #define NIL nil
  27. #endif
  28.  
  29. //=======================================================================
  30. TTalker::TTalker()
  31.     :    fSpeechChannel(NIL),
  32.         fComputerHasTextToSpeech(FALSE),
  33.         fNumVoices(0)
  34. {
  35.     fComputerHasTextToSpeech = TComputer::HasTextToSpeech();
  36.      if (fComputerHasTextToSpeech) {
  37.         OSErr err = ::NewSpeechChannel(NIL, &fSpeechChannel);
  38.         err = ::GetSpeechRate(fSpeechChannel, &fSpeechRate);
  39.         err = ::CountVoices(&fNumVoices);
  40.         }
  41. }
  42.  
  43. //----------------------------------------------------------------------
  44. TTalker::~TTalker()
  45. {
  46.     OSErr err = ::DisposeSpeechChannel(fSpeechChannel);
  47. }
  48.  
  49. //----------------------------------------------------------------------
  50. void
  51. TTalker::UseVoiceNamed(char* name)
  52. {
  53.     if (fComputerHasTextToSpeech) {
  54.         StringPtr pascalName = c2pstr(name);
  55.         OSErr err;
  56.         VoiceSpec spec;
  57.         VoiceDescription description;
  58.         for (short index = 0; index <= fNumVoices; index++) {
  59.             err = ::GetIndVoice(index, &spec);
  60.             err = ::GetVoiceDescription(&spec, &description, sizeof(description));
  61.             if ( ::EqualString(description.name, pascalName, FALSE, FALSE) ) {
  62.                 this->UseVoice(index);
  63.                 }    // if strcmp
  64.             }    // for
  65.         }    // if fComputerHasTextToSpeech
  66. }
  67.  
  68. //----------------------------------------------------------------------
  69. void
  70. TTalker::UseVoice(short index)
  71. {
  72.     if (fComputerHasTextToSpeech) {
  73.         OSErr err;
  74.         VoiceSpec spec;
  75.         VoiceDescription description;
  76.         long infoLength = sizeof(description);
  77.         err = ::GetIndVoice(index, &spec);
  78.         err = ::GetVoiceDescription(&spec, &description, infoLength);
  79.         err = ::DisposeSpeechChannel(fSpeechChannel);
  80.         fSpeechChannel = NIL;
  81.         err = ::NewSpeechChannel(&spec, &fSpeechChannel);
  82.         err = ::SetSpeechRate(fSpeechChannel, fSpeechRate);
  83.         }    // if fComputerHasTextToSpeech
  84. }
  85.  
  86. //----------------------------------------------------------------------
  87. char*
  88. TTalker::GetVoiceName(short index)
  89. {
  90.     OSErr err;
  91.     VoiceSpec spec;
  92.     static VoiceDescription description;
  93.     long infoLength = sizeof(description);
  94.     err = ::GetIndVoice(index, &spec);
  95.     err = ::GetVoiceDescription(&spec, &description, infoLength);
  96.     return p2cstr(description.name);
  97. }
  98.  
  99. //----------------------------------------------------------------------
  100. void
  101. TTalker::SayString(char* str, Boolean wait)
  102. {
  103.     long bytes = 0;
  104.     while (str[bytes])
  105.         bytes++;
  106.     SayText(str, bytes, wait);
  107. }
  108.  
  109. //----------------------------------------------------------------------
  110. void
  111. TTalker::SayFloat(float number, short digitsDesired, Boolean wait)
  112. {
  113.     decform format;
  114.     format.style = FIXEDDECIMAL;
  115.     format.digits = digitsDesired;
  116.     decimal numberDecimal;
  117.     ::num2dec(&format, number, &numberDecimal);
  118.     char numberString[40];
  119.     ::dec2str(&format, &numberDecimal, numberString);
  120.     SayString(numberString, wait);
  121. }
  122.  
  123. //----------------------------------------------------------------------
  124. void
  125. TTalker::SayInteger(long number, Boolean wait)
  126. {
  127.     const short kDigitsDesired = 0;
  128.     this->SayFloat(number, kDigitsDesired, wait);
  129. }
  130.  
  131. //----------------------------------------------------------------------
  132. void
  133. TTalker::SayTextEdit(TEHandle TEHdl, Boolean wait)
  134. {
  135.     Handle textHdl = (**TEHdl).hText;
  136.     long textBytes = ::GetHandleSize(textHdl);
  137.     ::HLock(textHdl);
  138.     SayText(*textHdl, textBytes, wait);
  139.     ::HUnlock(textHdl);
  140. }
  141.   
  142. //----------------------------------------------------------------------
  143. void
  144. TTalker::SayText(Ptr textPtr, long textBytes, Boolean wait)
  145. {    
  146.     // all speaking goes through this function
  147.     if (fComputerHasTextToSpeech) {
  148.         OSErr err = ::SpeakText(fSpeechChannel, textPtr, textBytes);
  149.         if (wait)
  150.             WaitUntilDone();
  151.     }    // if TComputer
  152. }
  153.  
  154. //----------------------------------------------------------------------
  155. void
  156. TTalker::WaitUntilDone()
  157. {
  158.     while (::SpeechBusy() )
  159.         {};
  160. }
  161.  
  162. //----------------------------------------------------------------------
  163. void
  164. TTalker::Faster()
  165. {
  166.     if (fComputerHasTextToSpeech) {
  167.         fSpeechRate *= 1.5;
  168.         OSErr err = ::SetSpeechRate(fSpeechChannel, fSpeechRate);
  169.     }
  170. }
  171.  
  172. //----------------------------------------------------------------------
  173. void
  174. TTalker::Slower()
  175. {
  176.     if (fComputerHasTextToSpeech) {
  177.         fSpeechRate /= 1.5;
  178.         ::SetSpeechRate(fSpeechChannel, fSpeechRate);
  179.         }
  180. }
  181.  
  182. //----------------------------------------------------------------------
  183. short
  184. TTalker::GetNumberVoices()
  185. {
  186.     return fNumVoices;
  187. }
  188.  
  189.